/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sp.panelcomponent;

import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import sp.model.TableUpdateModel;

/**
 *
 * @author HP
 */
public final class TableUpdate extends javax.swing.JPanel {

    /**
     * Creates new form TableUpdate
     */
    TableUpdateModel model;

    public TableUpdate() {
        initComponents();
        model = new TableUpdateModel();
        jScrollPane1.getViewport().setOpaque(false);
        setOpaque(false);
        tabelUpdate.setOpaque(false);
        setLebarKolom();
        setLebarKolom2();
//        coloringRow();
        sortTable();
    }

    public void setLebarKolom() {
        TableColumn column;
        this.tabelUpdate.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        column = this.tabelUpdate.getColumnModel().getColumn(0);
        column.setPreferredWidth(150);
        column = this.tabelUpdate.getColumnModel().getColumn(1);
        column.setPreferredWidth(150);
        column = this.tabelUpdate.getColumnModel().getColumn(2);
        column.setPreferredWidth(150);
        column = this.tabelUpdate.getColumnModel().getColumn(3);
        column.setPreferredWidth(50);
    }

    public void setLebarKolom2() {
        TableColumn column;
        this.tabelUpdate.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        column = this.tabelUpdate.getColumnModel().getColumn(0);
        column.setPreferredWidth(150);
        column = this.tabelUpdate.getColumnModel().getColumn(1);
        column.setPreferredWidth(150);
        column = this.tabelUpdate.getColumnModel().getColumn(2);
        column.setPreferredWidth(200);
    }

    public TableUpdateModel getModel() {
        return model;
    }

    public void setModel(TableUpdateModel model) {
        this.model = model;
    }

    public JTable getTabelUpdate() {
        return tabelUpdate;
    }

    public void setTabelUpdate(JTable tabelUpdate) {
        this.tabelUpdate = tabelUpdate;
    }

    private void sortTable() {
        tabelUpdate.setAutoCreateRowSorter(true);
        TableRowSorter<TableModel> sorter = new TableRowSorter<>(tabelUpdate.getModel());
        tabelUpdate.setRowSorter(sorter);
        List<RowSorter.SortKey> sortKeys = new ArrayList<>();

        int columnIndexToSort = 3;
        sortKeys.add(new RowSorter.SortKey(columnIndexToSort, SortOrder.DESCENDING));

        sorter.setSortKeys(sortKeys);
        for (int i = 0; i < 3; i++) {
            sorter.setSortable(i, false);
        }
        sorter.sort();
    }

    public void coloringRow() {
        tabelUpdate = new JTable(model) {
            public Component prepareRenderer(TableCellRenderer render, int indexRow, int indexCol) {
                Component comp = super.prepareRenderer(render, indexRow, indexCol);
                String status = (String) tabelUpdate.getModel().getValueAt(indexRow, 3);
                System.out.print(indexRow);
                System.out.println(status);
                if ("1".equals(status)) {
                    setBackground(Color.RED);
                    setForeground(Color.WHITE);
                }
                return comp;
            }
        };
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        sp.model.TableUpdateModel model = new sp.model.TableUpdateModel();
        tabelUpdate = new javax.swing.JTable(model);

        tabelUpdate.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tabelUpdate);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 556, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabelUpdate;
    // End of variables declaration//GEN-END:variables
}
